package com.utils;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;

public class DrawActiveImg {
	private static int WIDTH = 270;
	private static int HEIGHT = 28;
	private static BufferedImage image;
	private static String rands;

	public static String createRandom() {
		String str = "2345678wertuasdfhkzxcvbnm";
		char[] rands = new char[4];
		Random random = new Random();
		for (int i = 0; i < 4; i++) {
			rands[i] = str.charAt(random.nextInt(25));
		}
		return new String(rands);
	}

	public static void drawBackground(Graphics g) {
		// 画背景
		g.setColor(new Color(255, 255, 255));
		g.fillRect(0, 0, WIDTH, HEIGHT);
		// 随机产生 120 个干扰点
		for (int i = 0; i < 120; i++) {
			int x = (int) (Math.random() * WIDTH);
			int y = (int) (Math.random() * HEIGHT);
			int red = (int) (Math.random() * 255);
			int green = (int) (Math.random() * 255);
			int blue = (int) (Math.random() * 255);
			g.setColor(new Color(red, green, blue));
			g.drawOval(x, y, 1, 0);
		}
	}

	public static void drawRands(Graphics g, String rands) {
		g.setColor(new Color(75, 75, 75));
		g.setFont(new Font(null, Font.BOLD, 32));
		// 在不同的高度上输出验证码的每个字符
		g.drawString("" + rands.charAt(0), 5, 25);
		g.drawString("" + rands.charAt(1), 85, 25);
		g.drawString("" + rands.charAt(2), 135, 25);
		g.drawString("" + rands.charAt(3), 205, 25);
	}

	public static int getWIDTH() {
		return WIDTH;
	}

	public static void setWIDTH(int wIDTH) {
		WIDTH = wIDTH;
	}

	public static int getHEIGHT() {
		return HEIGHT;
	}

	public static void setHEIGHT(int hEIGHT) {
		HEIGHT = hEIGHT;
	}

	public static BufferedImage getImage() {
		rands = createRandom();
		image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();

		// 产生图像
		drawBackground(g);
		drawRands(g, rands);

		// 结束图像的绘制过程， 完成图像
		g.dispose();
		return image;
	}

	public static void setImage(BufferedImage image) {
		DrawActiveImg.image = image;
	}

	public static String getRands() {
		return rands;
	}

	public static void setRands(String rands) {
		DrawActiveImg.rands = rands;
	}
	
	
	

}
